package com.bgs.engineering.api.syslog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgs.engineering.api.syslog.mapper.SysLogExceptionMapper;
import com.bgs.engineering.api.syslog.service.SysLogExceptionService;
import com.bgs.engineering.util.BeanUtils;
import com.bgs.engineering.util.ObjectUtils;
import com.bgs.engineering.util.pageUtil.MybatisPageHelper;
import com.bgs.engineering.util.pageUtil.PageRequest;
import com.bgs.engineering.util.pageUtil.PageResult;
import com.bgs.engineering.util.validator.ValidationResult;
import com.bgs.engineering.util.validator.ValidatorUtil;
import com.bgs.engineering.api.syslog.entity.SysLogException;
import com.bgs.engineering.api.syslog.model.SysLogExceptionModel;
import com.bgs.engineering.common.errorCode.CommonException;
import com.bgs.engineering.common.errorCode.EmErrorCode;
import com.bgs.engineering.common.response.CommonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 异常日志服务层
 *
 * @auther zcy
 * @data 2023/3/22 13:35
 */
@Service
public class SysLogExceptionServiceImpl extends ServiceImpl<SysLogExceptionMapper, SysLogException> implements SysLogExceptionService {

  @Resource
  SysLogExceptionMapper sysLogExceptionMapper;

  /**
   * 分页查询异常日志
   *
   * @param pageRequest
   * @param sysLogExceptionModel
   * @return
   */
  @Override
  public PageResult selectSysLogExByPage(PageRequest pageRequest, SysLogExceptionModel sysLogExceptionModel) {
    MybatisPageHelper.startPage(pageRequest);
    return MybatisPageHelper.getPageResult(sysLogExceptionMapper.selectSysLogByPage(sysLogExceptionModel));
  }

  /**
   * 杀出日常日志
   *
   * @param id
   * @return
   * @throws CommonException
   */
  @Override
  public CommonResult deleteSysLogEXById(Integer id) throws CommonException {
    // 判断是否id为空
    if (ObjectUtils.isNotEmpty(id)) {
      int i = sysLogExceptionMapper.deleteById(id);
      // 判断是否操作成功
      if (i <= 0) {
        throw new CommonException(EmErrorCode.DELETE_ERROR);
      }
    } else {
      throw new CommonException(EmErrorCode.ID_NULL);
    }
    return CommonResult.ok(EmErrorCode.DELETE_OK);
  }

  /**
   * 根据id查询数据
   *
   * @param id
   * @return
   */
  @Override
  public SysLogException selectSysLogExById(Integer id) throws CommonException {
    // 判断是否id为空
    if (ObjectUtils.isNotEmpty(id)) {
      SysLogException sysLogException = sysLogExceptionMapper.selectById(id);
      // 判断是否操作成功
      if (ObjectUtils.isNotEmpty(sysLogException)) {
        return sysLogException;
      } else {
        throw new CommonException(EmErrorCode.ID_NOT_OBJECT);
      }
    } else {
      throw new CommonException(EmErrorCode.ID_NULL);
    }
  }

  /**
   * 根据时间段查询日志
   *
   * @param startTime
   * @param endTime
   * @return
   */
  @Override
  public Integer selectSysLogByTime(String startTime, String endTime) {
    Integer integer = sysLogExceptionMapper.selectSysLog(startTime, endTime);
    return integer;
  }

  /**
   * 保存异常日志
   *
   * @param sysLogExceptionModel
   * @return
   * @throws CommonException
   */
  @Override
  public CommonResult saveSysLogEx(SysLogExceptionModel sysLogExceptionModel) throws CommonException {
    if (ObjectUtils.isNotEmpty(sysLogExceptionModel)) {
      ValidationResult result = ValidatorUtil.validate(sysLogExceptionModel);
      if (result.isHasError()) {
        throw new CommonException(EmErrorCode.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
      }
      SysLogException sysLogException = new SysLogException();
      // sysLogExceptionModel 对象拷贝成 SysLogException
      BeanUtils.copy(sysLogExceptionModel, sysLogException);
      int i = sysLogExceptionMapper.insert(sysLogException);
      if (i > 0) {
        return CommonResult.ok(EmErrorCode.SAVE_OK);
      } else {
        return CommonResult.ok(EmErrorCode.SAVE_ERROR);
      }
    } else {
      // 返回前端异常数据
      throw new CommonException(EmErrorCode.OBJECT_NOT_NULL);
    }
  }
}
